package StudyCode.summary;

/**
 * @author lixiaoyao
 * @date 2018/12/22 0022、下午 1:16
 */
public class OpenURL {

	public static boolean isWindows() {
		String osName = System.getProperty("os.name");
		return osName.startsWith("Windows");
	}

	public static void main(String[] args) {
		String url = "www.baidu.com";
		Runtime runtime = Runtime.getRuntime();
		try {
			if (isWindows()) {
				runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
			} else {
				String[] browsers = {"chrome", "chromium-browser", "firefox", "opera",
						"epiphany", "mozilla", "konqueror", "netscape", "links", "lynx"};
				String browser = null;
				for (int count = 0; count < browsers.length && browser == null; count++)
					if (Runtime.getRuntime().exec(new String[]{"which", browsers[count]}).waitFor() == 0)
						browser = browsers[count];
				if (browser == null)
					throw new NoSuchMethodException("Could not find web browser");
				else
					Runtime.getRuntime().exec(new String[]{browser, url});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
